package com.cold.demo.generator.mybatis.utils;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zyc
 */
public class ModelToFileUtil {


    private static Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);

    public final static void createFile(String modelPath, String filePath, Map<String, Object> dataModel) {
        FileWriter out = null;
        try {
            cfg.setDirectoryForTemplateLoading(new File(modelPath).getParentFile());
            cfg.setDefaultEncoding("UTF-8");
            cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
            Template temp = cfg.getTemplate(new File(modelPath).getName());
            File     file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
            out = new FileWriter(file);
            temp.process(dataModel, out);
        } catch (TemplateException | IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void main(String[] args) {
        String modelPath = "E:\\software\\IdeaProjects\\demo-project\\demo-generator-mybatis\\src\\main\\java\\com\\cold\\demo\\generator\\mybatis\\utils\\document_temp.xml";
        String outPath = "E:\\software\\IdeaProjects\\demo-project\\demo-generator-mybatis\\src\\main\\java\\com\\cold\\demo\\generator\\mybatis\\utils\\document.xml";
        Map<String,Object> model = new HashMap<>();

        String excel = "E:/temp/无人机数据库表字段信息梳理.xlsx";
        List<List<Object>> dataAll = ExcelUtil.getReader(excel, 1).read();

        Data data = null;
        List<Data> d = new ArrayList<>();
        int i = 1;
        for (List<Object> list : dataAll) {
            if (list.get(0).toString().startsWith("t_")){
                data = new Data();
                d.add(data);
                data.setIndex(i+"");
                i++;
                data.setTableName(list.get(0).toString());
                data.setTableDesc(list.get(1).toString());
            }
            Data.Item item = new Data.Item();
            item.setFieldName(list.get(2).toString());
            item.setFieldType(list.get(3).toString());
            item.setFieldDesc(list.get(4).toString());
            item.setFieldAllowNull(list.get(5).toString());
            item.setFieldPri(list.get(6).toString());
            data.getList().add(item);
        }
        model.put("list",d);
        ModelToFileUtil.createFile(modelPath,outPath,model);
    }
}
